package com.lanou.zy.projecta.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.lanou.zy.projecta.R;
import com.lanou.zy.projecta.util.OnItemClickListener;
import com.lanou.zy.projecta.volley.VolleySingleton;
import com.lanou.zy.projecta.entity.FindNextStepAll;

import java.util.List;

/**
 * Created by dllo on 15/10/29.
 */
public class FindNextStepRecyclerViewAdapter extends RecyclerView.Adapter<FindNextStepRecyclerViewAdapter.FindNextStepViewHolder> {
    private ImageLoader imageLoader;
    private List<FindNextStepAll.DataEntity> fd;

    public FindNextStepRecyclerViewAdapter(Context c) {
        imageLoader = VolleySingleton.getInstance().getImageLoader();
    }

    public void addData(List<FindNextStepAll.DataEntity> datas) {
        fd = datas;
        notifyDataSetChanged();
    }


    @Override
    public FindNextStepViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        View v = inflater.inflate(R.layout.findnextstep_recycler_view_item, null);
        return new FindNextStepViewHolder(v);
    }


    @Override
    public void onBindViewHolder(final FindNextStepViewHolder holder, final int position) {
        if (fd != null && fd.size() > 0) {
            final FindNextStepAll.DataEntity f = fd.get(position);
            holder.findNextStepAll_Iv.setImageUrl(f.getPhoto(), imageLoader);

        }
        // 点击事件
        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getAdapterPosition();
                    mOnItemClickListener.onItemClick(holder.itemView, position);

                }
            });
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int position = holder.getAdapterPosition();
                    mOnItemClickListener.onItemLongClick(holder.itemView, position);
                    return false;
                }
            });
        }
    }


    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickLitener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }



    @Override
    public int getItemCount() {
        return fd != null && fd.size() > 0 ? fd.size() : 0;
    }

    class FindNextStepViewHolder extends RecyclerView.ViewHolder {
        public NetworkImageView findNextStepAll_Iv;

        public FindNextStepViewHolder(View v) {
            super(v);
            findNextStepAll_Iv = (NetworkImageView) v.findViewById(R.id.findNextStepAll_Iv);
        }

    }


}


